package com.shuizhu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.shuizhu.service.ExcelServiceImpl;
import com.shuizhu.service.ExcelServiceImpl2;
import com.shuizhu.template.demo5.Customer;
import com.shuizhu.template.demo5.ExcelMergeCellHandler;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @睡竹
 */
@Controller
public class ExcelController {

    @Autowired
    private ExcelServiceImpl service;
    @Autowired
    private ExcelServiceImpl2 service2;

    @PostMapping("/export")
    public void downExcel(HttpServletRequest reuqest, HttpServletResponse response){
         service.downExcel(reuqest,response);
    }

    @PostMapping("/export2")
    public void downExcel2(HttpServletRequest reuqest, HttpServletResponse response){
        service2.downExcel(reuqest,response);
    }


    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        // 查询需要导出的数据
        List result = getData();

        // 1设置表头样式
        WriteCellStyle headStyle = new WriteCellStyle();
        // 1.1设置表头数据居中
        headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 2设置表格内容样式
        WriteCellStyle bodyStyle = new WriteCellStyle();
        // 2.1设置表格内容水平居中
        bodyStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 2.2设置表格内容垂直居中
        bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 3设置表格sheet样式
        WriteSheet sheet = EasyExcel.writerSheet("客户信息").head(Customer.class).sheetNo(1).build();
        // 4拿到表格处理对象
        ExcelWriter writer = EasyExcel.write(response.getOutputStream()).needHead(true).excelType(ExcelTypeEnum.XLSX)
                // 设置需要待合并的行和列。参数1：数值数组，指定需要合并的列；参数2：数值，指定从第几行开始合并
                .registerWriteHandler(new ExcelMergeCellHandler(new int[]{0, 1, 2, 5}, 0))
                // 设置单元格的风格样式
                .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, bodyStyle))
                .build();

        // 5写入excel数据
        writer.write(result, sheet);
        // 6通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + new String("客户信息表".getBytes("gb2312"), "ISO8859-1") + ".xlsx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        writer.finish();
    }

    public static List<Customer> getData() throws Exception {
        List<Customer> data = new ArrayList<>();

        Customer customer = new Customer("JiangXi", "江西电信公司", "江西省南昌市东湖区", "张三", "12345678910", new URL("https://m.360buyimg.com/babel/jfs/t1/221733/11/14107/61280/62fde84dE467522ce/79bbd42aa93f5a83.jpg"));
        data.add(customer);
        Customer customer2 = new Customer("JiangXi", "江西电信公司", "江西省南昌市东湖区", "李四", "15848563521", new URL("https://m.360buyimg.com/babel/jfs/t1/221733/11/14107/61280/62fde84dE467522ce/79bbd42aa93f5a83.jpg"));
        data.add(customer2);

        Customer customer3 = new Customer("GuangDong", "广东电信公司", "广东省广州市花都区", "小明", "15847953624", new URL("https://m.360buyimg.com/babel/jfs/t1/215924/36/19623/23344/62baa985E4df523c6/4893237860b306d6.jpg"));
        data.add(customer3);
        Customer customer4 = new Customer("GuangDong", "广东电信公司", "广东省广州市天河区", "小红", "16849531548", new URL("https://m.360buyimg.com/babel/jfs/t1/189640/15/26493/35837/62baa97eE6abda209/461f91e682d0e81a.jpg"));
        data.add(customer4);
        Customer customer5 = new Customer("GuangDong", "广东电信公司", "广东省广州市天河区", "小华", "16985632481", new URL("https://m.360buyimg.com/babel/jfs/t1/189640/15/26493/35837/62baa97eE6abda209/461f91e682d0e81a.jpg"));
        data.add(customer5);

        Customer customer6 = new Customer("BeiJing", "北京电信公司", "北京市东城区", "姜维", "16598645874", new URL("https://m.360buyimg.com/babel/jfs/t1/31481/11/16081/24873/62baa97dE6f3991d0/94ae13d66b9bbfdd.jpg"));
        data.add(customer6);

        return data;
    }
}
